package diandiansong.library.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;

import diandiansong.library.R;

/**
 * Created by act on 2016/9/15.
 */
public class BaseFragAct extends BaseAct {

    private static final String KEY_FRAGMENT_NAME = "FrAGMENT_NAME";
    private static final String KEY_BUNDLE = "BUNDLE";
    private static final String KEY_TITLE = "TITLE";

    public static void start(Context context, String fragmentName, Bundle args, String title) {
        Intent starter = new Intent(context, BaseFragAct.class);
        starter.putExtra(KEY_FRAGMENT_NAME, fragmentName);
        starter.putExtra(KEY_BUNDLE, args);
        starter.putExtra(KEY_TITLE, title);
        context.startActivity(starter);
    }

    public static void start(Context context, String fragmentName, Bundle args) {
        start(context, fragmentName, args, null);
    }

    public static void start(Context context, String fragmentName, String title) {
        start(context, fragmentName, null, title);
    }

    public static void start(Context context, String fragmentName) {
        start(context, fragmentName, null, null);
    }


    FrameLayout mContentView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addContentView(R.layout.fragment_container);

        mContentView = (FrameLayout) findViewById(R.id.fragment_content);

        Intent intent = getIntent();
        String fragmentName = intent.getStringExtra(KEY_FRAGMENT_NAME);
        Bundle args = intent.getBundleExtra(KEY_BUNDLE);
        String title = intent.getStringExtra(KEY_TITLE);

        addFragment(fragmentName, args);

        if (title != null) {
            initTitle(title);
        }
    }


    protected void addFragment(String fragmentName, Bundle args) {
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = Fragment.instantiate(this, fragmentName);
        fragment.setArguments(args);
        fm.beginTransaction().add(mContentView.getId(), fragment).commit();
    }
}
